﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Semantics.Silverlight;
namespace SilverlightParser
{
    public partial class LearnLangNodesDialog : ChildWindow
    {
        List<LanguageNode> LangNodes;
        SilverWebService.LearnLangNodesJob Job;
        public LanguageNode Result;
        public LearnLangNodesDialog(SilverWebService.LearnLangNodesJob job)
        {
            this.Job=job;
            InitializeComponent();
            this.LangNodes = Bridge.ConvertToSilverLangNodeArray(job.LanguageNodes);
            References.RemotableMentorProxy.CreateWordCompletedEvent += new RemotableMentorProxy.CreateWordCompletedEventDelegate(RemotableMentorProxy_CreateWordCompletedEvent);
        }

        void RemotableMentorProxy_CreateWordCompletedEvent(int newwordid)
        {
            throw new NotImplementedException();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ChildWindow_Closed(object sender, EventArgs e)
        {

        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (LanguageNode langnode in this.LangNodes)
                this.languageNodeControl1.ShowRootLangNode(langnode);
        }

        List<TreeViewItem> selectedtreenodes = null;
        NodeDialog nodedlg = null;
        private void buttonCreateWord_Click(object sender, RoutedEventArgs e)
        {
            List<TreeViewItem> selectedtreenodes = this.languageNodeControl1.FindSelectedTreeNodes();

            string newword = "";
            foreach(TreeViewItem item in selectedtreenodes)
                newword += ((LanguageNode)item.Tag).Value;
            nodedlg = new NodeDialog(newword);
            nodedlg.Closed += new EventHandler(nodedlg_Closed);
            nodedlg.Show();
        }
        int count = 0;
        void nodedlg_Closed(object sender, EventArgs e)
        {
            if (nodedlg.DialogResult.HasValue && nodedlg.DialogResult.Value)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(delegate
                {
                    count++;
                    if (count > 1)
                        return;
                    try
                    {
                        int newnodeid = References.RemotableMentorProxy.CreateWord(nodedlg.Word, nodedlg.CategoryNodeId);
                    }
                    catch (Exception exp)
                    {
                        this.OnException(exp);
                    }
                });
            }
        }
        void OnException(Exception exp)
        {
            if (this.Dispatcher.CheckAccess())
            {
                this.ShowError(exp.ToString());
            }
            else
            {
                this.Dispatcher.BeginInvoke(delegate
                {
                    this.ShowError(exp.ToString());
                });
            }
        }

        void ShowError(string error)
        {
            MessageBox.Show(error);
        }
    }
}

